package com.jyxd.web.task;

import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.data.hisview.HisYb;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.service.assessment.basic.QuartzTimeService;
import com.jyxd.web.service.hisyb.HisybService;
import com.jyxd.web.util.CompareTimeUtil;
import com.jyxd.web.util.SpringUtil;
import com.jyxd.web.util.UUIDUtil;
import org.quartz.CronExpression;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.*;

/**
 * 从本数据库中同步数据到his的医保表
 */
public class YBJob implements Job {

    private static Logger logger= LoggerFactory.getLogger(YBJob.class);
    //定义一个成员变量
    private static QuartzTask quartzTask;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            logger.info("开始同步HIS医保数据=================");
            QuartzTime quartzTime=new QuartzTime();
            quartzTime.setStartTime(new Date());

            ServletContext context = null;
            //获取service
            context = (ServletContext) jobExecutionContext.getScheduler().getContext()
                    .get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTimeService quartzTimeService = (QuartzTimeService) cxt.getBean("quartzTimeService");
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");

            if(quartzTask==null){
                //为空时 表明第一次执行该方法 需要查询赋值
                Map<String,Object> map=new HashMap<>();
                map.put("jobName","YBJob");
                map.put("jobGroup","YBJob");
                quartzTask =quartzTaskService.queryDataByNameAndGroup(map);
            }
            HisybService hisybService = SpringUtil.getBean(HisybService.class);
            //查询本地的医保数据
            List<HisYb> list=hisybService.queryYBDataAll();
            List<HisYb> newList=new ArrayList<HisYb>();
            if(list!=null && list.size()>0) {
                //如果HIS数据库有数据不管，没有数据新增
                for (HisYb sourceData : list) {
                    HisYb hisYb = hisybService.queryData(sourceData.getDataid());
                    if (hisYb == null) {
                        newList.add(sourceData);
                    }
                }

                if (newList.size() > 0) {
                    //插入数据库
                    for (HisYb hisYb : newList) {
                        try {
                            hisybService.insert(hisYb);
                        }catch (Exception e){
                            logger.error("插入hisyb失败,dataId:{}",hisYb.getDataid());
                        }
                    }
                }
            }
            CronExpression expression = new CronExpression(quartzTask.getCron());
            quartzTime.setNextTime(expression.getNextValidTimeAfter(quartzTime.getStartTime()));
            quartzTime.setQuartzTaskId(quartzTask.getId());
            quartzTime.setId(UUIDUtil.getUUID());
            quartzTime.setEndTime(new Date());
            quartzTime.setTimeLength(String.valueOf(CompareTimeUtil.calculatetimeGapSecond(quartzTime.getStartTime(),quartzTime.getEndTime())));
            quartzTime.setStatus(1);
            quartzTimeService.insert(quartzTime);
            logger.info("HIS医保数据同步完毕==============");
        }catch (Exception e){
            logger.info("异常："+e);
        }
    }
}
